package com.kxf.androidtestdemo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kxf.androidtestdemo.R;

public class SysSettingsActivity extends BaseActivity {

    private static final String SettingName = "SettingNameTestkxf";
    private EditText et;
    private TextView tv_info;
    private StringBuffer sb = new StringBuffer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sys_settings);

        et = findViewById(R.id.et);
        tv_info = findViewById(R.id.tv_info);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            showInfo("写配置权限：" + Settings.System.canWrite(this.getApplicationContext()));
        }
    }

    public void readSetting(View view) {
        String str = Settings.System.getString(this.getApplicationContext().getContentResolver(), SettingName);
        showInfo("读取到的配置：" + str);
    }

    public void writeSetting(View view) {
        String txt = et.getText().toString().trim();
        if (TextUtils.isEmpty(txt)){
            showInfo("失败，数据为空");
        }else {
            boolean b = false;
            try {
                b = Settings.System.putString(this.getApplicationContext().getContentResolver(), SettingName, txt);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (b){
                showInfo("写入成功  " + txt);
            }else {
                showInfo("写入失败  " + txt);
            }
        }
    }

    private void showInfo(String str){
        sb.append(str + "\n\r");
        tv_info.setText(sb.toString());
    }
}